package com.ie.service.before;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.MD5Util;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.BUserEntity;
import com.ie.mapper.before.BUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
/**
 * @author xujia
 */
public class BUserServiceImpl extends ServiceImpl<BUserMapper, BUserEntity> implements BUserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;

    @Override
    public ResponseResult<Map<String, String>> register(BUserEntity bUserEntity) {
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        long n = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if (n > 0) {
            return ResponseResult.getMessageResult(null, "A001");
        } else if (save(bUserEntity)) {
            return ResponseResult.getMessageResult(null, "A002");
        } else {
            return ResponseResult.getMessageResult(null, "A003");

        }
    }

    @Override
    public ResponseResult<Map<String, String>> login(BUserEntity bUserEntity) {
        // 从Redis中获取验证码
        String code = (String) redisUtil.get("code");
        if (!code.equalsIgnoreCase(bUserEntity.getCode())) {
            return ResponseResult.getMessageResult(null, "A000");
        }
        // 从数据库中查询用户
        long res = lambdaQuery().eq(BUserEntity::getBemail, bUserEntity.getBemail()).count();
        if (res == 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        // 验证密码
        bUserEntity.setBpwd(MD5Util.MD5(bUserEntity.getBpwd()));
        List<BUserEntity> mu = lambdaQuery().eq(BUserEntity::getBpwd, bUserEntity.getBpwd())
                .eq(BUserEntity::getBemail, bUserEntity.getBemail()).list();
        if (mu.size() > 0) {
            //登录成功
            //根据用户登录的名称生成token
            String token = jwtTokenUtil.createToken(bUserEntity.getBemail());
            //buser用户信息保存到Redis
            redisUtil.set("login_"+bUserEntity.getBemail(),bUserEntity.getBemail(),config.getRedisExpiration());
            Map<String, String> myres = new HashMap();
            myres.put("buserauthtoken", token);
            myres.put("bemail", bUserEntity.getBemail());
            myres.put("bid", mu.get(0).getId()+"");
            return ResponseResult.getSuccessResult(myres);
        } else {
            //密码错误
            return ResponseResult.getMessageResult(null, "A002");
        }
    }
}
